<template>
	<view class="uni-tab-bar">
		<view class="uni-swiper-tab">
			
			<view key="jie" class="swiper-tab-list" :class="tabIndex == 0 ? 'active' : ''" id="jie" data-current="0" @click="tapTab1(0)">接单</view>
			<view key="jinxing" class="swiper-tab-list listtag" :class="tabIndex == 1 ? 'active' : ''" id="jinxing" data-current="1" @click="tapTab1(1)">
				进行中
				<text v-if="num">{{ num }}</text>
			</view>
		</view>
		<!-- <view class="uni-swiper-tab">
				<view v-for="(tab,index) in tabBars" :key="tab.id" class="swiper-tab-list" :class="tabIndex==index ? 'active' : ''" :id="tab.id"
			 :data-current="index" @click="tapTab">{{tab.name}}</view>
		</view> -->
		<view class="content">
			<view class="jiedan" v-if="tabIndex == 0">
				<view class="btn" v-if="flag" @tap="start">
					<image class="jieimg" src="../../static/images/icon/start.png"></image>
					<text>接单</text>
				</view>
				<view class="btn" v-if="!flag" @tap="stop">
					<image class="stopimg" src="../../static/images/icon/stop.png"></image>
					<text>停止</text>
				</view>
			</view>
			<view class="skxi" v-if="flag1">
				<view class="con">
					<image src="../../static/images/icon/jingtan.png"></image>
					<view class="tip">
						<view>没收到款千万不要确认</view>
						<view>造成损失自己负责</view>
					</view>
					<view class="jine"><input type="number" v-model="amount" placeholder="输入收款金额" /></view>
					<view class="confim">
						<view @click="quxiao">取消</view>
						<view class="blue" @tap="tijiao">确定</view>
					</view>
				</view>
			</view>
			<swiper :current="tabIndex" class="swiper-box" :duration="300" @change="changeTab">
				<swiper-item><qiangdan :current="tabIndex"></qiangdan></swiper-item>
				<swiper-item><list :current="tabIndex"></list></swiper-item>
			</swiper>
		</view>
	</view>
</template>
<script>
var timmer;
var timmer1;
var timmer2;
	import moment from "moment"
import { sendmsg,closeConnect,sendmsgheart} from '@/socket.js';
import { mapState } from 'vuex';
import list from '@/component/list/index.vue';
import qiangdan from '@/component/qiangdan/index.vue';
var _this;
export default {
	data() {
		return {
			flag1: false,
			account: '',
			amount: '',
			scrollLeft: 0,
			isClickChange: false,
			tabIndex: 0,
			flag: true,
			newsitems: [],
			num: 2,
			showflag: false,
			tabBars: [
				{
					name: '接单',
					id: 'jie'
				},
				{
					name: '进行中',
					id: 'jinxing'
				}
			]
		};
	},
	components: {
		list,
		qiangdan
	},
	watch: {
		orderunderway(newName, oldName) {
			this.numchange(newName);
		},
		heartmsg(newName, oldName) {
			this.changegheart(newName)
			// console.log(newName)	
		}
	},
	onLoad() {
		_this = this;
		this.num = this.orderunderway.length;
		this.$store.commit('order', []);
	},
	mounted() {
		this.$bus.$on('ifstart', data => {
			this.flag = data;
		});
		this.$bus.$on('tabfw', data => {
			this.tabIndex = 0;
		});
		this.$bus.$on('shou', data => {
			console.log(data);
			this.flag1 = data.flag;
			this.account = data.account;
		});
		var data = {
			token: uni.getStorageSync('token')
		};

		clearInterval(timmer);
		timmer = setInterval(()=> {
			this.$store.dispatch('orderunderway', data)
			.then(res=>{
				if(res.data.code==1){
					for(var i =0; i<this.orderunderway.length; i++){
						var h = (moment(this.orderunderway[i]['take_time']).unix()+  this.userinfo.time_interval*1) - moment().unix();
						
						if(h<=0){
							this.endOfTime(this.orderunderway[i]['order_id']);
						}
						
					}
				}
			})
			
			
		}, 3000);
		clearInterval(timmer2);
		timmer2 = setInterval(()=> {
			
			for(let i =0; i<this.order.length; i++){
				
				var h = (this.order[i][7] * 1+ this.userinfo.time_autotake*1) *1 - moment().unix() *1;
				
				if(h<=0){
					this.qiangdan(this.order[i][2]);
				}
			}
			
		}, 6000);
		this.$bus.$on('cleartimern', data => {
			clearInterval(timmer);
		});
		this.$bus.$on('cleartimer1', data => {
			clearInterval(timmer1);
		});
		this.$bus.$on('cleartimerq', data => {
			clearInterval(timmer2);
		});
	},
	onShow() {
		try {
			const value = uni.getStorageSync('tab_key');
			if (value) {
				if (value == true) {
					console.log(this.tabIndex);
					this.tabIndex = 2;
				}
				console.log(value);
			}
		} catch (e) {}
	
	},
	methods: {
		qiangdan(item){ 
					 // php接单
					 var data = {
					 	token: uni.getStorageSync('token'),
						order_id:item
					 };
					 this.$store.dispatch('takeorder', data).then(res => {
					 
						if(res.data.code==1){
							var data = {
								token: uni.getStorageSync('token'),
							};
							this.$store.dispatch('orderunderway', data)
						}
					 });
		},
		changegheart(item){
			
			this.$bus.$emit("cleartimer",1)
			var data;
			data={
				token: uni.getStorageSync('token')
			}
			// if(item == 0){
				
			// 	data={
			// 		token: uni.getStorageSync('token')
			// 	}
			// }
			// else if(item==1){
			// 	data={
			// 		token: uni.getStorageSync('token'),
			// 		is_back:1
			// 	}
			// }
			clearInterval(timmer1);
			timmer1 = setInterval(function() {
				_this.$store.dispatch('heart', data).then(res => {
					
					if (res.data.code == 0) {
						clearInterval(timmer1);
						clearInterval(timmer2);
						closeConnect();
						
						
						_this.$bus.$emit('cleartimern', 1);
						_this.$bus.$emit('cleartimer', 1);
			// #ifdef APP-PLUS
			_this.stopaudio();
			// #endif
						uni.showModal({
							title: '登录过期',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {
								uni.hideLoading();
								uni.removeStorage({
								    key: 'password',
								    success: function (res) {
								        
								    }
								});
								uni.reLaunch({
									url: '/pages/login/login'
								});
							}
						});
					}
					else if(res.data.code == 1){
						// console.log(8888888)
					}
				});
			}, 5000);
			
			clearInterval(timmer);
			timmer = setInterval(()=> {
				this.$store.dispatch('orderunderway', data)
				.then(res=>{
					if(res.data.code==1){
						for(var i =0; i<this.orderunderway.length; i++){
							var h = (moment(this.orderunderway[i]['take_time']).unix()+ this.userinfo.time_interval*1) - moment().unix();
							
							if(h<=0){
								this.endOfTime(this.orderunderway[i]['order_id']);
							}
							
						}
					}
				})
				
				
			}, 3000);
			clearInterval(timmer2);
			timmer2 = setInterval(()=> {
				
				for(let i =0; i<this.order.length; i++){
					console.log(this.order[i][7],moment().unix())
					var h = (this.order[i][7] * 1+  this.userinfo.time_autotake*1) *1 - moment().unix() *1;
					console.log(h)
					if(h<=0){
						this.qiangdan(this.order[i][2]);
					}
				}
				
			}, 6000);
			
			
			
			
		},
			endOfTime: function(item) {
			console.log(item);
			var data = {
				token: uni.getStorageSync('token'),
				order_id:item
			};
			this.$store.dispatch('ordertimeout', data)
			.then(res=>{
				console.log(res)
				if(res.data.code==1){
					var data = {
						token: uni.getStorageSync('token'),
					};
					this.$store.dispatch('orderunderway', data);
				}
			})
			
		},
		quxiao() {
			this.flag1 = false;
			this.amount = '';
		},
		tijiao() {
			if (this.amount) {
				var data = {
					token: uni.getStorageSync('token'),
					order_id: this.account.order_id,
					order_amount: this.amount
				};
				this.$store.dispatch('confirmreceitp', data).then(res => {
					this.amount = '';
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
					if (res.data.code == 1) {
						var data = {
							token: uni.getStorageSync('token')
						};
						this.$store.dispatch('orderunderway', data);
						this.flag1 = false;
					}
				});
			} else {
				uni.showToast({
					title: '请输入收款金额',
					icon: 'none'
				});
			}
		},
		shou(item) {
			this.flag = true;
			this.account = item;
		},
		numchange(newName) {
			this.num = newName.length;
		},
		start() {
			// php开始接单
			var data = {
				token: uni.getStorageSync('token')
			};
			this.$store.dispatch('grapstart', data).then(res => {
				uni.showToast({
					title: res.data.msg,
					icon:'none'
				});
				if (res.data.code == 1) {
					this.flag = false;
					try {
						uni.setStorageSync('ifjiedan', 1);
					} catch (e) {}
					if (this.userinfo.is_official == 1) {
						var data = {
							token: uni.getStorageSync('token')
						};
						this.$store.dispatch('orderallocated', data);
					}
				}
			});
		},
		stop() {
			// php结束接单
			// this.stopaudio()
			this.$store.commit("ifindex", true)
			var data = {
				token: uni.getStorageSync('token')
			};
			this.$store.dispatch('grapend', data).then(res => {
				try {
					uni.setStorageSync('ifjiedan', 0);
				} catch (e) {}
				uni.showToast({
					title: res.data.msg,
					icon: 'none'
				});
				if (res.data.code == 1) {
					this.flag = true;
				}
			});
		},
		changeTab(e) {
			let index = e.target.current;
			if (this.isClickChange) {
				this.tabIndex = index;
				this.isClickChange = false;
				return;
			}
			this.isClickChange = false;
			this.tabIndex = index; //一旦访问data就会出问题
		},

		tapTab(e) {
			//点击tab-bar
			let tabIndex = e.target.dataset.current;
			if (this.tabIndex === tabIndex) {
				return false;
			} else {
				this.isClickChange = true;
				this.tabIndex = tabIndex;
			}
		},
		tapTab1(index) {
			var tabIndex = index;
			if (this.tabIndex === tabIndex) {
				return false;
			} else {
				this.isClickChange = true;
				this.tabIndex = tabIndex;
			}
		}
	},
	computed: {
	...mapState([
		"orderunderway",
		"userinfo",
		"heartmsg",
		"order"
	]),
	}
	
};
</script>
<style lang="scss" scoped src="./index.scss"></style>
